package LearnAlgorithm.c_递归and查找排序;

public class j在有空字符串的有序字符串数组中查找 {

	public static void main(String[] args) {
		String[] arr = new String[] {"a","","ac","","ad","b","","ba"};
		System.out.println("index:" + search1(arr, "b"));
		System.out.println("index:" + search1(arr, "abc"));
		
	}
	
	private static int search1(String[] arr, String str) {
		int begin = 0;
		int end = arr.length-1;
		int mid = 0;
		while (begin <= end) {
			mid = begin + ((end - begin) >>> 1);
			while (arr[mid].equals("")) {
				if (mid + 1 > end) {
					System.out.println("要越界");
					return -1;
				}
				mid++;
			}
			if (arr[mid].compareTo(str) > 0) {
				end = mid -1;
			} else if (arr[mid].compareTo(str) < 0) {
				begin = mid + 1;
			} else {
				return mid;
			}
		}
		return -1;
	}
}
